<?php

class Install_IndexController extends My_Controller_Action
{

    public function init(){
    	
    }


     /********以下为自定义内容****************/
	function indexAction(){
			
	}
	
	function checkAction(){
		$paths = array(
				"data"=>ROOT_DIR."/data",
				"res/upload"=>ROOT_DIR."/res/upload"
			);
		$result = array();
		foreach ($paths as $k=>$v){
			$mode = Install_Tool::fileModeInfo($v);
			$result[$k] = $mode;
		}
		$this->view->path = $result;
	}
	
	function configAction(){
		if(My_Init::$isInstall){
    		header("Content-type: text/html; charset=utf-8");
    		exit('网站已经安装，请删除data/install.lock文件，重新安装!');
    	}
		error_reporting(0);
		if(My_Tool::isPost()){
			$dbHost = $this->p("DB_HOST");
			$dbPort = $this->p("DB_PORT");
			$dbName = $this->p("DB_NAME");
			$dbUser = $this->p("DB_USER");
			$dbPwd = $this->p("DB_PWD");
			
			$admin = $this->p("admin");
			$adminPwd = $this->p("adminPwd");
			
			$this->view->dbHost = $dbHost;
			$this->view->dbPort = $dbPort;
			$this->view->dbName = $dbName;
			$this->view->dbUser = $dbUser;
			$this->view->dbPwd = $dbPwd;
			$this->view->admin = $admin;
			$this->view->adminPwd = $adminPwd;
			
		   if(!$dbHost){
			 My_Tool_FlashMessage::set("mycmf_install","数据库地址为空!");
		    }elseif(!$dbPort){
				My_Tool_FlashMessage::set("mycmf_install","数据库端口为空!");
			}elseif(!$dbName){
				My_Tool_FlashMessage::set("mycmf_install","数据库名称为空!");
			}elseif(!$dbUser){
				My_Tool_FlashMessage::set("mycmf_install","数据库用户名为空!");
			}elseif(!$dbPwd){
				My_Tool_FlashMessage::set("mycmf_install","数据库密码为空!");
			}elseif(!$admin){
				My_Tool_FlashMessage::set("mycmf_install","管理员账户为空!");
			}elseif(!$adminPwd){
				My_Tool_FlashMessage::set("mycmf_install","管理员密码为空!");
			}
			$obj = new Install_Service_Common();
			$result = $obj->checkDbConn($dbHost, $dbPort, $dbUser, $dbPwd, $dbName);
			if($result !== true){
				My_Tool_FlashMessage::set("mycmf_install",$result);
			}
			
			if(!My_Tool_FlashMessage::has()){
					$obj->install($dbHost, $dbPort, $dbUser, $dbPwd, $dbName);
					//插入管理员数据
					$userObj = new User_Dao_Info();
					$data['username'] = $admin;
					$data['pwd'] = md5($adminPwd);
					$data['nickname'] = '管理员';
					$data['role'] = '10';
					$id = $userObj->insert($data);
					if($id) My_Tool::redirect($this->url('index/success'));
					My_Tool_FlashMessage::set("mycmf_install","未知错误请重试!");
			}
		}
		
	}
	
	function successAction(){
		
	}
	
	
}

